// common/common.js
let _request = null

export default {
	// 动态注入请求方法
	setRequestInstance(requestInstance) {
		_request = requestInstance
	},
	// apiHost: "http://7041vc1kl745.vicp.fun",
	// apiHost: "https://m.huishouka.com.cn",
	apiHost: "https://c.n.huishouka.com.cn",
	// imgApiHost: "https://7041vc1kl745.vicp.fun",
	// imgApiHost: "http://62.234.113.165",
	imgApiHost: "https://admin.huishouka.com.cn",
	title: "惠收卡",
	timeout: 10000,
	// 设置登录状态
	setLoginStorage: function($token) {
		uni.setStorageSync('token', $token);
		uni.setStorageSync('isLogin', true);
	},
	// 是否登录
	isLogin: function() {
		return uni.getStorageSync("isLogin") || false
	},
	// 获取token
	getToken: function() {
		return uni.getStorageSync("token")
	},
	// 是否注册上报
	isActivationRegist: function() {
		return uni.getStorageSync("isActivationRegist") || false
	},
	// 移除登录状态
	removeLoginStorage: function() {
		uni.removeStorageSync('token');
		uni.removeStorageSync('isLogin');
		uni.removeStorageSync('userInfo');
		uni.removeStorageSync('isReal');
		uni.removeStorageSync('isContract');
		uni.removeStorageSync('credentialLevel');
		uni.removeStorageSync('isSetFundPassWord');
		uni.removeStorageSync('mobile');
		uni.removeStorageSync('balance');
		uni.removeStorageSync('sellCardQuota');
		uni.removeStorageSync('canWithdrawBalance');
		uni.removeStorageSync('totalSettAmount');
		uni.removeStorageSync('totalFaceValue');
	},
	// 设置用户信息
	setUserInfo: function(user) {
		uni.setStorageSync('userInfo', user);
		uni.setStorageSync('isReal', user.isRealName);
		uni.setStorageSync('isContract', user.isSetContract);
		uni.setStorageSync('credentialLevel', user.credentialLevel);
		uni.setStorageSync('isSetFundPassWord', user.isSetFundPassWord);
		uni.setStorageSync('mobile', user.mobile);
		uni.setStorageSync('balance', user.balance);
		uni.setStorageSync('realName', user.name);
		uni.setStorageSync('idNum', user.idNum);
		uni.setStorageSync('sellCardQuota', user.sellCardQuota);
		uni.setStorageSync('canWithdrawBalance', user.canWithdrawBalance);
		uni.setStorageSync('totalSettAmount', user.totalSettAmount);
		uni.setStorageSync('totalFaceValue', user.totalFaceValue);
	},
	// 获取用户信息
	getUserInfo: function() {
		return uni.getStorageSync("userInfo")
	},
	// 获取实名状态
	isReal: function() {
		return uni.getStorageSync("isReal") || false
	},
	// 获取签署状态
	isContract: function() {
		return uni.getStorageSync("isContract") || false
	},
	// 获取认证等级
	getCredentialLevel: function() {
		return uni.getStorageSync("credentialLevel")
	},
	// 获取交易密码设置状态
	isSetFundPassWord: function() {
		return uni.getStorageSync("isSetFundPassWord") || false
	},
	// 获取手机号
	getUserMobile: function() {
		return uni.getStorageSync("mobile")
	},
	// 获取余额
	getUserBalance: function() {
		return uni.getStorageSync("balance")
	},
	// 获取可提现金额
	getCanWithdrawBalance: function() {
		return uni.getStorageSync("canWithdrawBalance")
	},
	// 获取累计销卡金额(已结算)
	getTotalSettAmount: function(){
		return uni.getStorageSync("totalSettAmount")
	},
	// 获取累计销卡面值
	getTotalFaceValue: function(){
		return uni.getStorageSync("totalFaceValue")
	},
	// 获取真实姓名
	getRealName: function() {
		return uni.getStorageSync("realName")
	},
	// 获取身份证
	getIdNum: function() {
		return uni.getStorageSync("idNum")
	},
	// 提交卡面值
	getSellCardQuota: function() {
		return uni.getStorageSync("sellCardQuota")
	},
	// 打开一个链接
	openUrl: function(url, needExternal) {
		if (needExternal) {
			// 跳转外部浏览器
			// #ifdef H5
			window.open(url, '_blank');
			// #endif
			// #ifdef APP-PLUS
			plus.runtime.openURL(url);
			// #endif
			// #ifdef MP-WEIXIN
			uni.showModal({
				content: '请复制链接到浏览器' ,
				success:(res) => {
					if(res.confirm){
						this.toCopy(url)
					}
				}
			});
			// #endif
		} else {
			// 应用内 Webview 打开
			uni.navigateTo({
				url: `/pages/webview/webview?url=${encodeURIComponent(url)}`
			});
		}
	},
	// 获取网站配置
	getAppConfit: async function() {
		// 先尝试读取本地缓存
		const cache = uni.getStorageSync('appConfig');
		console.log("本地配置", cache)
		if (cache) return cache;

		const res = await _request({
			method: 'GET',
			url: '/config/web',
		});
		if (res) {
			uni.setStorageSync('appConfig', res.data);
			return res.data;
		}
	},
	// 是否正式版本
	getIsOffiacal: async function() {
		// #ifdef MP-WEIXIN
		const res = await _request({
			method: 'GET',
			url: '/version',
		});
		if (res) {
			if (res.data == '1') {
				return false;
			} else {
				return true;
			}

		} else {
			return false;
		}
		// #endif

		// #ifndef MP-WEIXIN
		return true;
		// #endif
	},
	// 获取热门卡券
	getHotCardList: async function(tabId) {
		const res = await _request({
			method: 'GET',
			url: '/product/product/list',
			data: {
				prodTypeId: tabId
			}
		});

		if (res) {
			return res.data;
		}
	},
	// 复制
	toCopy: function(content) {
		uni.setClipboardData({
			data: content
		})
	},
	// 时间格式转换
	formateDate: function(time) {
		// 使用Date对象解析时间字符串
		const date = new Date(time);

		// 格式化日期为yyyy-MM-dd hh:mm:ss的形式
		const y = date.getFullYear();
		let m = date.getMonth() + 1; // Months are zero indexed
		let d = date.getDate();
		let h = date.getHours();
		let min = date.getMinutes();
		let s = date.getSeconds();

		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		min = min < 10 ? '0' + min : min;
		s = s < 10 ? '0' + s : s;

		return `${y}-${m}-${d} ${h}:${min}:${s}`;
	},
	toast: function(message) {
		uni.showToast({
			title: message,
			icon: 'none'
		});
	},
	showLoading: function(message){
		uni.showLoading({
			title: message,
			mask:true
		})
	},
	hideLoading: function(){
		uni.hideLoading();
	},
	navTo: function(page){
		uni.navigateTo({
			url: `/pages/${page}/${page}`
		});
	}
};